package com.example.myblog1_0.mapper;

import com.example.myblog1_0.model.Article;
import net.sf.json.JSONObject;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: xc
 * @Date: 2018/12/15 18:38
 * @Description:
 **/
@Mapper
@Repository
public interface ArticleMapper {
    /**
     * 根据文章标题查询文章
     * @param title 文章标题
     * @return 查询到的对象
     */
    @Select("select * from article where title=#{title}")
    Article findArticleByTitle(String title);

    /**
     * 按时间查找文章
     * @param time 时间
     * @return
     */
    @Select("select * from article where writeTime = #{time}")
    Article findArticleByTime(String time);
    /**
     *  按降序排列所有文章
     * @return 文章对象集合
     */
    @Select("select * from article order by id desc")
    List<Article> findAllArticleDesc();

    /**
     * 插入文章数据
     * @param article 文章对象
     * @return 插入成功返回true
     */
    @Insert("insert into article(title, articleContent, writeTime,summary,articleUrl,articleCategories) values(#{title}, #{articleContent}, #{writeTime},#{summary},#{articleUrl},#{articleCategories})")
    boolean insetArticle(Article article);

    /**
     * 根据id查找文章名称
     * @param id
     * @return
     */
    @Select("select * from article where id = #{id}")
    String findArticleById(String id);

    /**
     * 根据id获得文章
     * @param id
     * @return
     */
    @Select("select * from article where id = #{id} ")
    Article getArticleByArticleId(String id);

    @Select("select id,articleCategories,title,writeTime from article order by id desc")
    List<Article> getAllArticlePartInfo();

    @Select("select id,articleCategories,title,writeTime from article where articleCategories = #{categoriesName}")
    List<Article> getArticlePartInfoByCategory(String categoriesName);

    @Select("select id,articleCategories,title,writeTime from article order by id desc")
    List<Article> getArticleManage();

    @Delete("delete from article where id = #{id}")
    boolean deleteArticleById(String id);

    @Update("update article set articleUrl= #{articleUrl} where writeTime = #{writeTime}")
    boolean updateUrlByTime(Article article);

    @Update("update article set title= #{title},articleContent=#{articleContent},writeTime=#{writeTime} where id = #{id}")
    boolean updateArticleByID(Article article);
}
